package com.devshephard.rpcframework.rpc.container;

import com.devshephard.rpcframework.rpc.factory.channel.SingletonClientMultiChannelFactory;
import io.netty.channel.Channel;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 全局容器
 */
public class GlobalContainer {

    //频道容器,启动时将会以饿汉式创建多个C/S端连接并存入容器当中
    public static ConcurrentHashMap<String, Channel> channelMap;

    //频道所属启动类容器,启动时将会以饿汉式创建多个C/S端连接并存入容器当中
    public static ConcurrentHashMap<String, SingletonClientMultiChannelFactory> channelFactoryMap;

    //spring applicationContext 上下文
    public static ApplicationContext applicationContext;

    //@RpcClient注解标记类容器,标记了上述注解的类都会放入该容器中
    public static Set<BeanDefinition> rpcClientAnnoClassSet;

    //@RpcClient注解信息容器
    public static List<Map<String,Object>> rpcClientAnnoInfoList;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        GlobalContainer.applicationContext = applicationContext;
    }

    public static Set<BeanDefinition> getRpcClientAnnoClassSet() {
        return rpcClientAnnoClassSet;
    }

    public static void setRpcClientAnnoClassSet(Set<BeanDefinition> rpcClientAnnoClassSet) {
        GlobalContainer.rpcClientAnnoClassSet = rpcClientAnnoClassSet;
    }

    public static List<Map<String, Object>> getRpcClientAnnotationInfoList() {
        return rpcClientAnnoInfoList;
    }

    public static ConcurrentHashMap<String, Channel> getChannelContainerMap() {
        return channelMap;
    }

    public static ConcurrentHashMap<String, SingletonClientMultiChannelFactory> getChannelFactoryContainerMap() {
        return channelFactoryMap;
    }

    public static void putChannelInContainerMap(String addressDetail, Channel channel) {
        //TODO 可能要处理一下不能重复,或者其他的...
        channelMap.put(addressDetail,channel);
    }

    public static void putChannelFactoryInContainerMap(String addressDetail, SingletonClientMultiChannelFactory singletonClientMultiChannelFactory) {
        //TODO 可能要处理一下不能重复,或者其他的...
        channelFactoryMap.put(addressDetail,singletonClientMultiChannelFactory);
    }
}
